package com.blockmeta.bbs.businesslibrary.widget.chartview.Axis;

import com.blankj.utilcode.util.TimeUtils;
import com.blockmeta.bbs.businesslibrary.widget.chartview.CandleData;
import com.blockmeta.bbs.businesslibrary.widget.chartview.ChartData;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Layer.Layer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class MarketRankCoordinate extends Coordinate {
    public SimpleDateFormat m_5YearFormat = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    public void setData(ChartData pData) {
        List<Point> tXList = new ArrayList<Point>();
        List<Point> tYList = new ArrayList<Point>();

        if (pData != null) {
            ChartData.ChartPeroid mChartType = pData.getChartPeroid();
            Layer.ChartTypeEnum tLayerType = m_callback.getCharTypeEnum();
            m_ScaleFactor = m_callback.getScaleFactor();

            List<CandleData> tDataList = pData.getCandleData();

            int tStartIndex ;//为数据index的位置
            m_nStartIndex = pData.m_dataIndex;

            int tMaxIndex = 0;
            int tMinIndex = 0;

            tSymBol = pData.getSymbol();

            int minCount;


            tStartIndex = 0;
            m_nStartIndex = 0;
            minCount = pData.getCandleData().size();
            m_nQueryCount = pData.getCandleData().size();//为一屏幕显示的个数

            m_bDigit = 2;//价格小数位数
            if (tSymBol != null) {
                m_bDigit = tSymBol.getDigit();
            }

            for (int i = tStartIndex; i < minCount; i++) {

                CandleData tData = tDataList.get(i);

                int tHigh = -1;
                int tLow = -1;

                tHigh = (int) tData.m_lVolume;
                tLow =  (int) tData.m_lVolume;


                if (i == tStartIndex) {
                    tMaxIndex = tHigh;
                    tMinIndex = tLow;
                } else {
                    if (tHigh >= tMaxIndex) {
                        tMaxIndex = tHigh;
                    }
                    if (tLow <= tMinIndex) {
                        tMinIndex = tLow;
                    }
                }

                if(i % 2 != 0)
                {

                    String max = m_5YearFormat.format(TimeUtils.millis2Date(tData.getDate()));
                    tXList.add(new Point(0, 0, 0, 0, true, max, (i + 1 - tStartIndex)));
                }

            }


            m_nYIntervalCount = 4;

            int diff = tMaxIndex - tMinIndex;
            int i = 0;
            do{
                i++;
            }
            while (diff > m_nYIntervalCount * i);
            int range = m_nYIntervalCount * i;
            int upper = tMaxIndex, buttomer = tMinIndex;

            boolean addUpper = false;
            while ((upper - buttomer) != range) {
                if (addUpper) {
                    upper++;
                }
                else if(buttomer > 0)
                {
                    buttomer--;
                }
                addUpper = !addUpper;
            }

            m_nYIntervalStandard = (upper - buttomer) / (m_nYIntervalCount);

            int cur = upper;
            for (i = 0; i <= m_nYIntervalCount; i++) {
                String max = String.valueOf(cur);

                if(i % 2 == 0) {
                    tYList.add(new Point(0, 0, 0, 0, true, max, i + 1));
                }
                else {
                    tYList.add(new Point(0, 0, 0, 0, true, "", i + 1));
                }
                cur -= m_nYIntervalStandard;
            }



            m_fMaxValue = upper;
            m_fMinValue = buttomer;
        }
        m_xAxis.setPointList(tXList);
        m_yAxis.setPointList(tYList);
    }

}
